/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package cpasl;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.MalformedURLException;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.VPos;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.RowConstraints;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;

/**
 * This group consists of a text area on the left, a video on the right,
 * and a "next" button on the bottom.
 * 
 * @author seanm_000
 */
public class InstructionsGroup extends GridPane {
    
    private MediaPlayer mediaPlayer = null;
    
    public InstructionsGroup(File textFile, File videoFile, Groups next) {
        setHgap(40);
        setVgap(40);
        setPadding(new Insets(40));
        
        ColumnConstraints column1 = new ColumnConstraints();
        column1.setHalignment(HPos.LEFT);
        getColumnConstraints().add(column1); 
        ColumnConstraints column2 = new ColumnConstraints();
        column2.setHalignment(HPos.RIGHT);
        getColumnConstraints().add(column2); 
        RowConstraints row1 = new RowConstraints();
        row1.setValignment(VPos.TOP);
        getRowConstraints().add(row1);
        try {
            BufferedReader textReader = new BufferedReader(
                    new FileReader(textFile));
            String fullText = "";
            String line = textReader.readLine();
            while (line != null) {
                fullText += line + "\n";
                line = textReader.readLine();
            }
            Label label = new Label(fullText);
            label.setWrapText(true);
            label.setId("instructions-label");
            add(label, 0, 0);
            try {
                Media media = new Media(videoFile.toURI().toURL().toString());
                mediaPlayer = new MediaPlayer(media);
                mediaPlayer.setOnEndOfMedia(new Runnable() {

                    @Override
                    public void run() {
                        mediaPlayer.stop();
                    }
                    
                });
                // create mediaView and add media player to the viewer
                MediaView mediaView = new MediaView(mediaPlayer);
                add(mediaView, 1, 0);
            }
            catch (MalformedURLException mue) {
                mue.printStackTrace();
            }
        }
        catch (IOException ioe) {
            ioe.printStackTrace();
        }
        
        Button nextButton = new Button("Next");
        nextButton.setId("next-button");
        nextButton.setOnAction(new NextActionListener(next));
        add(nextButton, 0, 1);
        
        Button replayButton = new Button("Replay");
        replayButton.setId("replay-button");
        replayButton.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                mediaPlayer.play();
            }
            
        });
        add(replayButton, 1, 1);
    }
    
    public void play() {
        mediaPlayer.play();
    }
    
    private class NextActionListener implements EventHandler<ActionEvent> {
        
        private Groups next = null;
        
        public NextActionListener(Groups next) {
            this.next = next;
        }

        @Override
        public void handle(ActionEvent e) {
            CpaslManager.getInstance().switchToDisplay(next, 0);
        }
    }
    
}
